﻿using System;
using System.Windows.Forms;
using HRM.HRMServiceProxy;
using System.Data;
using System.Drawing;

namespace HRM
{
    public partial class frmPremiumInfo : FormBase
    {
        PremiumInfo _PremiumInfo;
        PremiumClient _wcfPremium;
        EmployeeClient _wcfEmployee;
        DetailClient _wcfDetail;
        DetailInfo _DetailInfo;
        int _EmployeeID;
        int _PremiumID;
        bool isChange = false;
        DialogResult dlgResult;
        RoleRightInfo listRole;
        int _DetailID = 0;
        public delegate void AfterCreateOrSaveItem();
        public event AfterCreateOrSaveItem AfterCreateEvent;

        public frmPremiumInfo(int PremiumID, int EmployeeID, int DetailID, RoleRightInfo _listRole)
        {
            InitializeComponent();
            txtWorkUnit.GotFocus +=new EventHandler(txtWorkUnit_GotFocus);

            LoadLanguage();
            dlgResult = DialogResult.Cancel;
            _PremiumInfo = new PremiumInfo();
            _wcfPremium = Service.Premium();
            _wcfEmployee = Service.Employee();
            _EmployeeID = EmployeeID;
            _PremiumID = PremiumID;
            _DetailID = DetailID;
            listRole = _listRole;
            txtDateVal.ReadOnly = true;
            txtDateVal.BackColor = Color.White;
            DateTextBoxShort dtbFrom = new DateTextBoxShort(mtxFromDate);
            DateTextBoxShort dtbTo = new DateTextBoxShort(mtxToDate);

            //-------------------------------------------------------------------

            btnSaveAndNew.Enabled = listRole.Create;
            btnSave.Enabled = listRole.Update;            

            txtSIBookNo.Enabled = false;

            //Sửa
            if (_DetailID > 0)
            {
                btnSave.Enabled = false;
                TableDetailInfo2Form(_DetailID);
            }
            //
            else if (PremiumID > 0)
            {
                this.Mode = FormMode.Update;
                //
                _PremiumInfo = _wcfPremium.Premium_GetInfo(PremiumID);
                Info2Form();
                
                //Hiển thị quá trình trước đây của số sổ
                qtTruocDay.QuaTrinhTruocDay(_PremiumInfo.SIBookNo);
                //
                //
                //qtTruocDay.SelectedRow(_PremiumID);
            }
            else //Thêm mới
            {
                this.Mode = FormMode.Insert;
                //
                txtDateVal.Text = DateTime.Now.Date.ToString("dd/MM/yyyy");
                if (_EmployeeID > 0)
                {
                    txtSIBookNo.Text = _wcfEmployee.Employee_GetInfo(_EmployeeID).SIBookNo.Trim();
                    //
                    timSoBHXH1.Text = txtSIBookNo.Text;
                    timSoBHXH1.Tag = timSoBHXH1.Text;
                    timSoBHXH1.SelectAll();
                }
                //
                //qtTruocDay.DSNhapMoi();
                qtTruocDay.QuaTrinhTruocDay(txtSIBookNo.Text);
            }
            //
            qtTruocDay.suaThongTinQuaTrinh += new Common.QT_TruocDay.SuaThongTinQuaTrinh(qtTruocDay_suaThongTinQuaTrinh);
        }

        public DialogResult Show_Form()
        {
            return this.ShowDialog();
        }

        protected override void Init_Control()
        {
            base.Init_Control();
        }

        /// <summary>
        /// Hiển thị thông tin được chọn để sửa
        /// </summary>
        /// <param name="Premium_Id"></param>
        void qtTruocDay_suaThongTinQuaTrinh(int Premium_Id, int Employee_Id)
        {
            _PremiumID = Premium_Id;
            _EmployeeID = Employee_Id;
            _PremiumInfo = _wcfPremium.Premium_GetInfo(_PremiumID);
            Info2Form();
            //qtTruocDay.SelectedRow(_PremiumID);
        }

        public void LoadLanguage()
        {
            if (this.Tag != null)
                this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_Panel(ref panel2);
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }

        void TableDetailInfo2Form(int _ID)
        {
            _wcfDetail = Service.Detail();
            _DetailInfo = _wcfDetail.Detail_GetInfo(_ID);

            txtSIBookNo.Text = _DetailInfo.InsBookNo;
            mtxFromDate.Text = _DetailInfo.FromDate.ToString("MM/yyyy");
            mtxToDate.Text = _DetailInfo.ToDate.ToString("MM/yyyy");
            //lblMonth.Text = _PremiumInfo.MonthNo.ToString();
            txtWorkUnit.Text = Program.strBranchCode;
            txtWorkName.Text = Program.strBranch;
            txtJob.Text = "";
            if (_DetailInfo.Type == "HS")
                radHSL.Checked = true;
            else if (_DetailInfo.Type == "ML")
                radML.Checked = true;
            else if (_DetailInfo.Type == "CT")
                radCT.Checked = true;
            txtMinSalary.Text = "0";
            switch (_DetailInfo.InsTypeCode)
            { 
                case "0":
                    radkhong.Checked = true;
                    radTS.Checked = radOF.Checked = false;
                    break;
                case "TS":
                    radTS.Checked = true;
                    radkhong.Checked = radOF.Checked = false;
                    break;
                case "OF":
                    radOF.Checked = true;
                    radTS.Checked = radkhong.Checked = false;
                    break;
            }
           
            txtSalary.Text = _DetailInfo.NewAmount.ToString("#,##0");
            txtBasicFactor.Text = _DetailInfo.NewBasicFactor.ToString("#,##0.0#");
            txtPositionFactor.Text = _DetailInfo.NewPositionFactor.ToString("#,##0.0#");
            txtSeniorityFactor.Text = _DetailInfo.NewJobFactor.ToString("#,##0.0#");
            txtAreaFactor.Text = _DetailInfo.NewAreaFactor.ToString("#,##0.0#");
            txtOverSeniorityFactor.Text = _DetailInfo.NewOverFactor.ToString("#,##0.0#");
            txtDescription.Text = "";
            txtDateVal.Text = _DetailInfo.FromDate.ToString("dd/MM/yyyy");
            isChange = false;

            //Gán value cho control tìm số sổ
            timSoBHXH1.Text = _DetailInfo.InsBookNo;
            timSoBHXH1.Tag = timSoBHXH1.Text;
            timSoBHXH1.EmployeeId = _EmployeeID;
        }

        void Info2Form()
        {
            txtSIBookNo.Text = _PremiumInfo.SIBookNo;
            mtxFromDate.Text = _PremiumInfo.FromDate.ToString("MM/yyyy");
            mtxToDate.Text = _PremiumInfo.ToDate.ToString("MM/yyyy");
            lblMonth.Text = _PremiumInfo.MonthNo.ToString();
            
            switch(_PremiumInfo.WorkUnit.ToUpper()){
                case "HW0004G":
                    radHW004G.Checked=true;
                    break;
                case "HW0005G":
                    radHW005G.Checked = true;
                    break;
                default:
                    txtWorkUnit.Text = _PremiumInfo.WorkUnit;
                    break;
            }
            txtWorkName.Text = _PremiumInfo.WorkName;
            txtJob.Text = _PremiumInfo.Job;
            if (_PremiumInfo.Type == "HS")
                radHSL.Checked = true;
            else if (_PremiumInfo.Type == "ML")
                radML.Checked = true;
            else if (_PremiumInfo.Type == "CT")
                radCT.Checked = true;
            txtMinSalary.Text = _PremiumInfo.BasicSalary.ToString("#,##0");

            switch (_PremiumInfo.InsTypeCode)
            {
                case "0":
                    radkhong.Checked = true;
                    radTS.Checked = radOF.Checked = false;
                    break;
                case "TS":
                    radTS.Checked = true;
                    radkhong.Checked = radOF.Checked = false;
                    break;
                case "OF":
                    radOF.Checked = true;
                    radTS.Checked = radkhong.Checked = false;
                    break;
            }
            txtSalary.Text = _PremiumInfo.Salary.ToString("#,##0");
            txtBasicFactor.Text = _PremiumInfo.BasicFactor.ToString(UIParamater.Cham3So);
            txtPositionFactor.Text = _PremiumInfo.PositionFactor.ToString(UIParamater.Cham3So);
            txtSeniorityFactor.Text = _PremiumInfo.SeniorityFactor.ToString(UIParamater.Cham3So);
            txtAreaFactor.Text = _PremiumInfo.AreaFactor.ToString(UIParamater.Cham3So);
            txtOverSeniorityFactor.Text = _PremiumInfo.OverSeniorityFactor.ToString(UIParamater.Cham3So);
            txtDescription.Text = _PremiumInfo.Description;
            
            isChange = false;
            //
            //Gán value cho control tìm số sổ
            timSoBHXH1.Text = _PremiumInfo.SIBookNo;
            timSoBHXH1.Tag = timSoBHXH1.Text;
            timSoBHXH1.EmployeeId = _EmployeeID;

            //
            //txtDateVal.Text = _PremiumInfo.DateVal == DateTime.MinValue ? "" : _PremiumInfo.DateVal.ToString("dd/MM/yyyy");
            txtDateVal.Text = DateTime.Now.ToString("dd/MM/yyyy");
        }

        void Form2Info()
        {
            _PremiumInfo.ID = _PremiumID;
            _PremiumInfo.EmployeeID = _EmployeeID;
            _PremiumInfo.SIBookNo = txtSIBookNo.Text;
            _PremiumInfo.FromDate = Convert.ToDateTime(mtxFromDate.Text);
            _PremiumInfo.ToDate = Convert.ToDateTime(mtxToDate.Text);
            _PremiumInfo.MonthNo = UISetting.GetMonth(_PremiumInfo.FromDate, _PremiumInfo.ToDate);

            if (radHW004G.Checked) _PremiumInfo.WorkUnit = "HW0004G";
            else if (radHW005G.Checked) _PremiumInfo.WorkUnit = "HW0005G";
            else _PremiumInfo.WorkUnit = txtWorkUnit.Text;

            _PremiumInfo.WorkName = txtWorkName.Text;
            _PremiumInfo.Job = txtJob.Text;
            if (radHSL.Checked)
                _PremiumInfo.Type = "HS";
            else if (radML.Checked)
                _PremiumInfo.Type = "ML";
            else if (radCT.Checked)
                _PremiumInfo.Type = "CT";

            
            if (radkhong.Checked)
                _PremiumInfo.InsTypeCode = "0";
            else if (radTS.Checked)
                _PremiumInfo.InsTypeCode = "TS";
            else if (radOF.Checked)
                _PremiumInfo.InsTypeCode = "OF";
            _PremiumInfo.BasicSalary = UISetting.GetDecimalNo(txtMinSalary.Text);
            _PremiumInfo.Salary = UISetting.GetDecimalNo(txtSalary.Text);
            _PremiumInfo.BasicFactor = UISetting.GetDecimalNo(txtBasicFactor.Text);
            _PremiumInfo.PositionFactor = UISetting.GetDecimalNo(txtPositionFactor.Text);
            _PremiumInfo.SeniorityFactor = UISetting.GetDecimalNo(txtSeniorityFactor.Text);
            _PremiumInfo.AreaFactor = UISetting.GetDecimalNo(txtAreaFactor.Text);
            _PremiumInfo.OverSeniorityFactor = UISetting.GetDecimalNo(txtOverSeniorityFactor.Text);
            _PremiumInfo.Description = txtDescription.Text.Trim();
            
            //if (txtDateVal.Text == "  /  /")
            //    _PremiumInfo.DateVal = Convert.ToDateTime("01/01/2000");
            //else
            //    _PremiumInfo.DateVal = UISetting.GetDate(txtDateVal.Text);

            _PremiumInfo.DateVal = UISetting.GetDate(txtDateVal.Text);
        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {
            if (!CheckTextValid.IsDateTime(mtxFromDate.Text, "MM/yyyy"))
            {
                mtxFromDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngaybatdaukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!CheckTextValid.IsDateTime(mtxToDate.Text, "MM/yyyy"))
            {
                mtxToDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngayketthuckhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (DateTime.Compare(UISetting.GetDate(mtxFromDate.Text), UISetting.GetDate(mtxToDate.Text)) > 0)
            {
                mtxFromDate.Focus();
                MessageBox.Show(UpdateUI.GetString("ngayketthucphaimuonhonngaybatdau"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (CheckTextValid.IsEmpty(txtWorkName.Text))
            {
                txtWorkName.Focus();
                MessageBox.Show(UpdateUI.GetString("donvilamviectrong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }

            if (!CheckTextValid.IsDecimal(txtSalary.Text))
            {
                txtSalary.Focus();
                MessageBox.Show(UpdateUI.GetString("tienluongkhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!CheckTextValid.IsDecimal(txtPositionFactor.Text))
            {
                txtPositionFactor.Focus();
                MessageBox.Show(UpdateUI.GetString("hspcchucvukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!CheckTextValid.IsDecimal(txtSeniorityFactor.Text))
            {
                txtSeniorityFactor.Focus();
                MessageBox.Show(UpdateUI.GetString("hspcthamniennghekhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            if (!CheckTextValid.IsDecimal(txtOverSeniorityFactor.Text))
            {
                txtOverSeniorityFactor.Focus();
                MessageBox.Show(UpdateUI.GetString("hspcthamnienvuotkhungkhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return false;
            }
            //if (_PremiumID == 0 && _DetailID == 0)
            //{
            //    if (DateTime.Compare(UISetting.GetDate(mtxFromDate.Text), UISetting.GetDate("01/12/2008")) >= 0)
            //    {
            //        mtxFromDate.Focus();
            //        MessageBox.Show("Chỉ được nhập thời gian trước tháng 12/2008!", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //        return false;
            //    }
            //}
            if (radCT.Checked == radHSL.Checked == radML.Checked == false)
            {
                MessageBox.Show("Chưa chọn hình thức đóng bảo hiểm", UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                radHSL.Focus();
                return false;
                
            }
            //Kiểm tra đến tháng phải <= tháng nhỏ nhất trong quá trình phát sinh trên hệ thống
            //_wcfDetail = Service.Detail();
            //if (!_wcfDetail.KiemTraGiaTriThangPhatSinh(txtSIBookNo.Text, UISetting.GetDate(mtxToDate.Text)))
            //{
            //    MessageBox.Show("Quá");
            //    return false;
            //}
             

            //hiepdh: 13/05/2014
            //Đến tháng của phát sinh quá trình trước đây, ko được > từ tháng nhỏ nhất trong quá trình BHXH
            //hiện tại ko bắt, nên trong quá trình trước đây vẫn nhập giao được với quá trình BHXH đang đóng là sai
            //lúc đó quá trình trước đây lại hiển thị cả quá trình hiện tại, và chia đoạn cso thể bị sai


            return true;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            PremiumClient _wcfPremium = Service.Premium();

            Form2Info();

            //try
            //{
                if (_PremiumID > 0)//update
                {
                    if (!listRole.Update)
                    {
                        MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    if (_wcfPremium.Premium_Update(_PremiumInfo))
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        isChange = false;
                        dlgResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        isChange = true;
                    }
                }
                else//insert
                {
                    if (!listRole.Create)
                    {
                        MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    if (_DetailID == 0)
                        _PremiumID = _wcfPremium.Premium_Insert(_PremiumInfo);
                    if (_PremiumID > 0)
                    {
                        Program.taskbarNotifier.Show(UpdateUI.GetString("thongbao"), UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        isChange = false;
                        dlgResult = DialogResult.OK;
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        isChange = true;
                    }
                }
            //}
            //catch (Exception ex)
            //{
            //    MessageBox.Show(ex.ToString());
            //}

            DateTime _toDate = DateTime.ParseExact(mtxToDate.Text, "MM/yyyy", null);
            mtxFromDate.Text = _toDate.AddMonths(1).ToString("MM/yyyy");
            mtxToDate.Text = "";
            //mtxToDate.Focus();
            _PremiumID = 0;

            //radkhong.Checked = true;
            //
            if (AfterCreateEvent != null)
                AfterCreateEvent();

            //Hiển thị danh sách dữ liệu quá trình trước đây bên phải
            //if (Mode == FormMode.Insert)
                //qtTruocDay.DSNhapMoi();
                qtTruocDay.QuaTrinhTruocDay(timSoBHXH1.Text);
            //else
            //{
            //    qtTruocDay.QuaTrinhTruocDay(timSoBHXH1.Text);
            //}
                mtxFromDate.Focus();
                mtxFromDate.SelectAll();
        }

        private void btnNew_Click(object sender, EventArgs e)
        {

        }

        private void frmPremiumInfo_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            _wcfPremium.Close();
            this.DialogResult = dlgResult;
        }

        private void frmPremiumInfo_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Enter:
                    //if (!e.Alt && !e.Control)
                    //{
                    //    this.ProcessTabKey(!e.Shift);
                    //    e.Handled = true;
                    //}
                    break;
                case Keys.Escape:
                    this.Close();
                    break;
                case Keys.F2:
                    btnSaveAndNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
            }
        }

        private void txtSalary_Leave(object sender, EventArgs e)
        {
            txtSalary.Text = UISetting.GetDecimalNo(txtSalary.Text).ToString(UIParamater.Phay2So);
        }

        private void txtMonthNo_Leave(object sender, EventArgs e)
        {
            lblMonth.Text = UISetting.GetIntNo(lblMonth.Text).ToString();
        }

        private void txtPositionFactor_Leave(object sender, EventArgs e)
        {
            txtPositionFactor.Text = UISetting.GetDecimalNo(txtPositionFactor.Text).ToString(UIParamater.Cham2So);
        }

        private void txtSeniorityFactor_Leave(object sender, EventArgs e)
        {
            txtSeniorityFactor.Text = UISetting.GetDecimalNo(txtSeniorityFactor.Text).ToString(UIParamater.Cham2So);
        }

        private void txtAreaFactor_Leave(object sender, EventArgs e)
        {
            txtAreaFactor.Text = UISetting.GetDecimalNo(txtAreaFactor.Text).ToString(UIParamater.Cham2So);
        }

        private void txtOverSeniorityFactor_Leave(object sender, EventArgs e)
        {
            txtOverSeniorityFactor.Text = UISetting.GetDecimalNo(txtOverSeniorityFactor.Text).ToString(UIParamater.Cham2So);
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }

        private void mtxToDate_Leave(object sender, EventArgs e)
        {
            //if (mtxToDate.Text != "  /")
            //    if (!CheckTextValid.IsDateTime(mtxFromDate.Text, "MM/yyyy"))
            //    {
            //        mtxFromDate.Focus();
            //        MessageBox.Show(UpdateUI.GetString("ngaybatdaukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //        return;
            //    }
            //    else if (!CheckTextValid.IsDateTime(mtxToDate.Text, "MM/yyyy"))
            //    {
            //        mtxToDate.Focus();
            //        MessageBox.Show(UpdateUI.GetString("ngayketthuckhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
            //        return;
            //    }
            //    else
            //    {
            //        DateTime _date1 = DateTime.ParseExact(mtxFromDate.Text, "MM/yyyy", null);
            //        DateTime _date2 = DateTime.ParseExact(mtxToDate.Text, "MM/yyyy", null);
            //        lblMonth.Text = (UISetting.GetMonth(_date1, _date2) + 1).ToString();
            //    }
        }

        private void radML_CheckedChanged(object sender, EventArgs e)
        {
            if (radML.Checked)
            {
                txtSalary.Enabled = true;
                txtBasicFactor.Enabled = false;
                txtPositionFactor.Enabled = false;
                txtSeniorityFactor.Enabled = false;
                txtOverFactor_Percent.Enabled = false;
                txtJobFactor_Percent.Enabled = false;
                txtOverSeniorityFactor.Enabled = false;
                txtAreaFactor.Enabled = false;
            }
            else
            {
                txtSalary.Text = "0";
                txtSalary.Enabled = false;
            }
        }

        private void radHSL_CheckedChanged(object sender, EventArgs e)
        {
            if (radHSL.Checked)
            {
                txtBasicFactor.Enabled = true;
                txtPositionFactor.Enabled = true;
                txtOverFactor_Percent.Enabled = true;
                txtJobFactor_Percent.Enabled = true;
                txtSeniorityFactor.Enabled = true;
                txtOverSeniorityFactor.Enabled = true;
                txtAreaFactor.Enabled = true;
                txtSalary.Enabled = false;
            }
            else
            {
                txtBasicFactor.Enabled = false;
                txtPositionFactor.Enabled = false;
                txtSeniorityFactor.Enabled = false;
                txtOverSeniorityFactor.Enabled = false;
                txtAreaFactor.Enabled = false;

                txtOverFactor_Percent.Enabled = false;
                txtJobFactor_Percent.Enabled = false;

                txtBasicFactor.Text = "0";
                txtPositionFactor.Text = "0";
                txtSeniorityFactor.Text = "0";
                txtOverSeniorityFactor.Text = "0";
                txtAreaFactor.Text = "0";
            }
        }

        private void mtxToDate_Validated(object sender, EventArgs e)
        {
            if (mtxToDate.Text.Trim() != "")
            {
                if (mtxToDate.Text != "  /")
                    if (!CheckTextValid.IsDateTime(mtxFromDate.Text, "MM/yyyy"))
                    {
                        mtxFromDate.Focus();
                        MessageBox.Show(UpdateUI.GetString("ngaybatdaukhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else if (!CheckTextValid.IsDateTime(mtxToDate.Text, "MM/yyyy"))
                    {
                        mtxToDate.Focus();
                        MessageBox.Show(UpdateUI.GetString("ngayketthuckhonghople"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    else
                    {
                        DateTime _date1 = DateTime.ParseExact(mtxFromDate.Text, "MM/yyyy", null);
                        DateTime _date2 = DateTime.ParseExact(mtxToDate.Text, "MM/yyyy", null);
                        lblMonth.Text = (UISetting.GetMonth(_date1, _date2) + 1).ToString();
                    }
            }
        }

        private void radCT_CheckedChanged(object sender, EventArgs e)
        {
            if (radCT.Checked)
            {
                txtBasicFactor.Enabled = false;
                txtPositionFactor.Enabled = false;
                txtSeniorityFactor.Enabled = false;
                txtOverFactor_Percent.Enabled = false;
                txtJobFactor_Percent.Enabled = false;
                txtAreaFactor.Enabled = false;
                txtOverSeniorityFactor.Enabled = false;
                txtSalary.Enabled = false;
                txtMinSalary.Enabled = false;
            }
            else
            {
            }
        }

        private void radHW04G_CheckedChanged(object sender, EventArgs e)
        {
            if (radHW004G.Checked)
            {
                txtWorkUnit.Text = radHW004G.Text;
            }
        }

        private void radHW05G_CheckedChanged(object sender, EventArgs e)
        {
            if (radHW005G.Checked)
            {
                txtWorkUnit.Text = radHW005G.Text;
            }
        }

        private void txtWorkUnit_MouseEnter(object sender, EventArgs e)
        {
            
        }

        void txtWorkUnit_GotFocus(object sender, EventArgs e)
        {
            //txtWorkUnit.BackColor = System.Drawing.Color.White;
            //pnlDonViKXD.BackColor = System.Drawing.Color.Gray;
            radHW004G.Checked = radHW005G.Checked = false;
        }

        private void txtWorkUnit_Leave(object sender, EventArgs e)
        {
            if (txtWorkUnit.Text.ToUpper() == Program.strBranchBHXH.ToUpper())
                txtWorkName.Text = Program.strBranch;
        }

        private void btnSaveAndNew_Click(object sender, EventArgs e)
        {
            btnSave_Click(null, null);
            //DateTime _toDate = DateTime.ParseExact(mtxToDate.Text, "MM/yyyy", null);
            mtxFromDate.Text = "";// _toDate.AddMonths(1).ToString("MM/yyyy");
            mtxToDate.Text = "";
            //mtxToDate.Focus();
            _PremiumID = 0;
          
            radkhong.Checked = true;
            if (AfterCreateEvent != null)
                AfterCreateEvent();
            //
            mtxFromDate.Focus();
        }

        private void radkhong_CheckedChanged(object sender, EventArgs e)
        {
            if (radkhong.Checked)
                txtJob.Text = "";            
        }

        private void radTS_CheckedChanged(object sender, EventArgs e)
        {
            if (radTS.Checked)
                txtJob.Text = "Nghỉ thai sản theo chế độ"; 
        }

        private void radOF_CheckedChanged(object sender, EventArgs e)
        {
            if (radOF.Checked)
                txtJob.Text = "Nghỉ không lương/Nghỉ ốm"; 
        }

        private void txtOverFactor_Percent_Validated(object sender, EventArgs e)
        {
            if (txtOverSeniorityFactor.Text.Length == 0)
                txtOverSeniorityFactor.Text = "0";
            //
            decimal BasicFactor = UISetting.GetDecimalNo(txtBasicFactor.Text);
            decimal Percent_OverFactor = UISetting.GetDecimalNo(txtOverFactor_Percent.Text);
            txtOverSeniorityFactor.Text = ((Percent_OverFactor * BasicFactor) / 100).ToString(UIParamater.Cham3So);

            //
            Calculate();
        }

        void Calculate()
        {
            decimal _MinSalary = 0;
            decimal _BasicFactor = 0;
            decimal _PositionFactor = 0;
            decimal _OverSeniorityFactor = 0;
            decimal _SeniorityFactor = 0;
            decimal _AreaFactor = 0;
            decimal _BasicAmount = 0;
            decimal _PositionAmount = 0;
            decimal _OverAmount = 0;
            decimal _JobAmount = 0;
            decimal _AreaAmount = 0;
            decimal _PhoneAmount = 0;
            decimal _LunchAmount = 0;
            decimal _OtherAmount = 0;
            decimal _Total = 0;
            _MinSalary = UISetting.GetDecimalNo(txtMinSalary.Text);
            _BasicFactor = UISetting.GetDecimalNo(txtBasicFactor.Text);
            _PositionFactor = UISetting.GetDecimalNo(txtPositionFactor.Text);
            _OverSeniorityFactor = UISetting.GetDecimalNo(txtOverSeniorityFactor.Text);
            _SeniorityFactor = UISetting.GetDecimalNo(txtSeniorityFactor.Text);
            _AreaFactor = UISetting.GetDecimalNo(txtAreaFactor.Text);
            //_BasicAmount = UISetting.GetDecimalNo(txtBasicSalary.Text);
            //_PositionAmount = UISetting.GetDecimalNo(txtPositionAllowance.Text);
            //_OverAmount = UISetting.GetDecimalNo(txtOverAllowance.Text);
            //_JobAmount = UISetting.GetDecimalNo(txtJobAllowance.Text);
            //_AreaAmount = UISetting.GetDecimalNo(txtAreaAllowance.Text);
            //_PhoneAmount = UISetting.GetDecimalNo(txtTelephone.Text);
            //_LunchAmount = UISetting.GetDecimalNo(txtLunch.Text);
            //_OtherAmount = UISetting.GetDecimalNo(txtOther.Text);
            //_Total = (_BasicFactor + _PositionFactor + _OverFactor + _JobFactor + _AreaFactor) * _MinSalary + _BasicAmount + _PositionAmount + _OverAmount + _JobAmount + _AreaAmount + _PhoneAmount + _LunchAmount + _OtherAmount;

            //Format so
            //txtBasicSalary.Text = _BasicAmount.ToString("#,##0");
            //txtPositionAllowance.Text = _PositionAmount.ToString("#,##0");
            //txtOverAllowance.Text = _OverAmount.ToString("#,##0");
            //txtJobAllowance.Text = _JobAmount.ToString("#,##0");
            //txtAreaAllowance.Text = _AreaAmount.ToString("#,##0");
            //txtTelephone.Text = _PhoneAmount.ToString("#,##0");
            //txtLunch.Text = _LunchAmount.ToString("#,##0");
            //txtOther.Text = _OtherAmount.ToString("#,##0");
            //txtTotal.Text = _Total.ToString("#,##0");
        }

        private void txtJobFactor_Percent_Validated(object sender, EventArgs e)
        {
            if (txtJobFactor_Percent.Text.Length == 0)
                txtJobFactor_Percent.Text = "0";
            //
            decimal BasicFactor = UISetting.GetDecimalNo(txtBasicFactor.Text);
            decimal PositionFactor = UISetting.GetDecimalNo(txtPositionFactor.Text);
            decimal Percent_JobFactor = UISetting.GetDecimalNo(txtJobFactor_Percent.Text);
            decimal OverSeniorityFactor = UISetting.GetDecimalNo(txtOverSeniorityFactor.Text);
            decimal SeniorityFactor = (BasicFactor + PositionFactor + OverSeniorityFactor) * Percent_JobFactor / 100;

            //
            txtOverSeniorityFactor.Text = OverSeniorityFactor.ToString(UIParamater.Cham3So);
            txtSeniorityFactor.Text = SeniorityFactor.ToString(UIParamater.Cham3So);
            //
            Calculate();
        }

        //Kiểm tra thay đổi số sổ thì load lại danh sách quá trình trước đây
        private void timSoBHXH1_Validated(object sender, EventArgs e)
        {
            if (Globals.Object_GetString(timSoBHXH1.Tag, "") != timSoBHXH1.Text && timSoBHXH1.Text != ""
                && timSoBHXH1.EmployeeId > 0
                )
            {
                FormGlobals.App_ProgressShow(delegate()
                {
                    qtTruocDay.QuaTrinhTruocDay(timSoBHXH1.Text);
                }, "Loading");
            }
        }

        private void btnDel_Click(object sender, EventArgs e)
        {
            if (qtTruocDay.SelectedRow !=null)
            {
                if (!listRole.Delete)
                {
                    MessageBox.Show(UpdateUI.GetString("khongcoquyenxoa"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    return;
                }
                _PremiumID = Convert.ToInt32(qtTruocDay.SelectedRow.Cells["Premium_ID"].Value);
                if (MessageBox.Show(UpdateUI.GetString("bancochacchanxoa"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    if (_wcfPremium.Premium_Delete(_PremiumID))
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("xoathanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        //LoadPremium(_InsBookNo);
                        qtTruocDay.QuaTrinhTruocDay(timSoBHXH1.Text);
                        _PremiumID = 0;
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("xoakhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                    }
                }
            }
        }

        private void btnNew_Click_1(object sender, EventArgs e)
        {
            timSoBHXH1.Text = "";
            timSoBHXH1.EmployeeId = 0;

            mtxFromDate.Text = "";// _toDate.AddMonths(1).ToString("MM/yyyy");
            mtxToDate.Text = "";
            txtDateVal.Text = DateTime.Now.ToString("dd/MM/yyyy");
            timSoBHXH1.Focus();
        }
    }
}
